import com.alibaba.fastjson.JSON;
import com.tc.controller.UserInfoAjax;
import com.tc.entity.Student;
import com.tc.entity.TestMapperBean1;
import com.tc.entity.TestMapperBean2;
import com.tc.entity.UserInfo;
import com.tc.service.UserInfoSerivce;
import com.tc.support.BeanMapperHelper;
import lombok.extern.java.Log;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//import sun.misc.Unsafe;

import javax.validation.spi.ValidationProvider;
import java.util.*;

/**
 * Created by cai.tian on 2017/5/9.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/mvc-servlet.xml" }, inheritLocations = true)
@ActiveProfiles(value = "test")
@Log
public class test3 {
    @Autowired
    private UserInfoAjax userInfoAjax;

    @Autowired
    private UserInfoSerivce userInfoSerivce;
    
    @Test
    public void demo(){
        String a = userInfoAjax.getPublicKey(null);
        System.out.println(a);
    }

    @Test
    public void demo1(){
        List<UserInfo> loginname = userInfoAjax.sortAndFind(null,"", "loginname","");
        System.out.println(JSON.toJSONString(loginname));
    }

    /**
     * 测试 cas 查看是否其他线程修改过
     */
    //@Test
    public void demo2(){
        //Unsafe unsafe = Unsafe.getUnsafe();
        //byte aByte = unsafe.getByte(1);
       // System.out.println(aByte);
    }




    /**
     * 测试lombok
     */

    @Test
    public void demo3(){
        Student s=new Student();
        s.getName();
        log.info("lombok test");
        //System.out.println(s.getName());
        //s.getName();
    }

    /**
     * 测试bean的转换
     */
    @Test
    public void demo4(){
        TestMapperBean1 testMapperBean1 =new TestMapperBean1();
        testMapperBean1.setAmount(100l);
        testMapperBean1.setAmount2(200l);

        TestMapperBean2 testMapperBean2 = BeanMapperHelper.getInstance().map(testMapperBean1, TestMapperBean2.class);

        log.info( ""+testMapperBean2.getAmount());
        log.info( ""+testMapperBean2.getAmount2());
        //System.out.println(s.getName());
        //s.getName();
    }


    @Test
    public void demo5(){//resources/META-INF/services
        Map map = new HashMap();
        map.put(new a(9,2),"abcd");
        map.put(new a(9,1),"abcdS");

        for (Object v : map.values()) {
            System.out.println("value= " + v);
        }
        ServiceLoader<UserInfoSerivce> loader = ServiceLoader.load( UserInfoSerivce.class, test3.class.getClassLoader() );
        Iterator<UserInfoSerivce> iterator = loader.iterator();
        UserInfoSerivce next = iterator.next();



        List<UserInfo> userInfos = next.find(null);
        System.out.println(JSON.toJSONString(userInfos));
    }

    class a{
        int hashCode = 0;
        int i = 0;
        public a(int hashCode,int i){
            this.hashCode=hashCode;
            this.i=i;
        }
        @Override
        public int hashCode(){
            return hashCode;
        }
        @Override
        public boolean equals(Object obj) {
            if(obj instanceof a){
                return ((a) obj).i==i;
            }
            return super.equals(obj);
        }
    }



}
